package com.mkz.storage.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkz.storage.entity.Storage;
import com.mkz.storage.mapper.StorageMapper;
import com.mkz.storage.service.StorageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库存表
 *
 * @author Kuangjianyu
 * @date 2020-05-06 13:55:11
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements StorageService {

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Boolean deduct(String commodityCode, Integer count) {
		Storage storage = baseMapper.selectOne(Wrappers.<Storage>query().lambda().eq(Storage::getCommodityCode, commodityCode));
		if (storage.getCount() < count) {
			throw new RuntimeException("超过库存数，扣除失败！");
		}
		storage.setCount(storage.getCount() - count);
		baseMapper.updateById(storage);
		return true;
	}
}
